<?php

namespace app\useradmin\controller;

use app\BaseController;
use app\useradmin\controller\Comics\User\AddUsersAndConfirmations;
use app\useradmin\controller\Comics\User\ComicsEdit;
use app\useradmin\controller\Comics\User\ModifyYourOwnPermissions;
use app\useradmin\controller\Comics\User\UserDeleteUser;
use app\useradmin\controller\Comics\User\UserInheritedUser;
use think\facade\Db;
use think\facade\View;

class AuthorAndChineseTeam extends BaseController
{
    public function ModifyYourOwnPermissions()
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id
        $author_and_chinese_team_id = randIdStructure(1); // 生成一个id 这张表author_and_chinese_team 中的_id 对应 author_and_chinese_team_operation 中的——id

        $postData = parseUserInfo(
            $postData,
            false,
        );
        $postData = json_decode($postData->getContent(), true);

        if ($postData['code'] == 200) {
            $postData = $postData['data'];
        } else {
            return $postData;
        }
        //解析原数据 
        $ModifyYourOwnPermissions = new ModifyYourOwnPermissions($this->app);
        return $ModifyYourOwnPermissions->enter($user_info, $postData, $author_and_chinese_team_id);
    }
    public function ComicsInfoDeleteUser() //退出删除用户
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id
        $postData = parseUserInfo(
            $postData,
            true
        );
        $postData = json_decode($postData->getContent(), true);
        $UserDeleteUser = new UserDeleteUser($this->app);
        return $UserDeleteUser->deleteUser($user_info, $postData);
    }
    public function InheritedUsersAreAllowedToJoin()
    {
        $UserInheritedUser = new UserInheritedUser($this->app);
        return $UserInheritedUser->InheritedUsersAreAllowedToJoin();
    }
    public function RefuseToInherit()
    {
        $UserInheritedUser = new UserInheritedUser($this->app);
        return $UserInheritedUser->RefuseToInherit();
    }
    public function AgreeToDeleteUser() //同意删除用户
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true
        );
        $postData = json_decode($postData->getContent(), true);
        $UserDeleteUser = new UserDeleteUser($this->app);
        return $UserDeleteUser->AgreeToDeleteUser($user_info, $postData);
    }
    public function RefuseToDeleteUser() //拒绝删除用户
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true
        );
        $postData = json_decode($postData->getContent(), true);
        $UserDeleteUser = new UserDeleteUser($this->app);
        return $UserDeleteUser->RefuseToDeleteUser($user_info, $postData);
    }
    public function RecoveryUser() //恢复删除用户
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id
        /*
         *{
         * "initListType": ["1"],
         * "initListUser_id": 前台传入的要操作的用户的json 双重验证,
         * "toKen": 前台传入的 我自己的token 只可加密 获取自己的token + 书籍id +时间戳 取sha256,
         * "bookId": 书籍id,
         * "time": 时间戳,
         * "verificationBookId": 书籍id 加密方法 书籍id+时间戳取sha256 , 
         * "DecodeListUser_Info": 解密要操作的用户id 第一次解密用,
         * "jsonDecodeUserInfo": {用base64 解码 DecodeListUser_Info 转换为 json //这是用户新数据 请注意如果是转移用户信息 数据只有  hash_hmac_user_id time user_id
         *     "hash_hmac_user_id": sha256加密的字符串,
         *     "user_id": "5af393a7db91d02c83d987b4",
         *     "time": 1589301799,
         *     "_id":列表id
         *     "book_id": "5c8941bbb9497270670798e1"
         *     "role":"角色 0创作者 1 协作者"
         *   }
         * "DecodeinitListUserInfo": //用户元数据解析 需要与 DecodeListUser_Info 解析的一致 就代表一个用户
         * }
         * 
         * "jsonDecodeinitListUserInfo": {用base64 解码 DecodeinitListUserInfo 转换为 json //这是用户原数据
         *     "hash_hmac_user_id": sha256加密的字符串,
         *     "user_id": "5af393a7db91d02c83d987b4",
         *     "time": 1589301799,
         *     "_id":列表id
         *     "book_id": "5c8941bbb9497270670798e1"
         *     "role":"角色 0创作者 1 协作者"
         *   }
         */
        $postData = parseUserInfo(
            $postData,
            true
        );
        $postData = json_decode($postData->getContent(), true);

        $UserDeleteUser = new UserDeleteUser($this->app);
        return $UserDeleteUser->RecoveryUser($user_info, $postData);
    }
    public function AddUser()
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.AddUser_id')[0]; //修改的用户id
        $postData['newListUser_id'] = input('post.AddUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        /*  
         *{
         * "initListType": ["1"],
         * "initListUser_id": 前台传入的要操作的用户的json 双重验证,
         * "toKen": 前台传入的 我自己的token 只可加密 获取自己的token + 书籍id +时间戳 取sha256,
         * "bookId": 书籍id,
         * "time": 时间戳,
         * "verificationBookId": 书籍id 加密方法 书籍id+时间戳取sha256 , 
         * "DecodeListUser_Info": 解密要操作的用户id 第一次解密用,
         * "jsonDecodeUserInfo": {用base64 解码 DecodeListUser_Info 转换为 json //这是用户新数据 请注意如果是转移用户信息 数据只有  hash_hmac_user_id time user_id
         *     "hash_hmac_user_id": sha256加密的字符串,
         *     "user_id": "5af393a7db91d02c83d987b4",
         *     "time": 1589301799,
         *     "_id":列表id
         *     "book_id": "5c8941bbb9497270670798e1"
         *     "role":"角色 0创作者 1 协作者"
         *   }
         * "DecodeinitListUserInfo": //用户元数据解析 需要与 DecodeListUser_Info 解析的一致 就代表一个用户
         * }
         * 
         * "jsonDecodeinitListUserInfo": {用base64 解码 DecodeinitListUserInfo 转换为 json //这是用户原数据
         *     "hash_hmac_user_id": sha256加密的字符串,
         *     "user_id": "5af393a7db91d02c83d987b4",
         *     "time": 1589301799,
         *     "_id":列表id
         *     "book_id": "5c8941bbb9497270670798e1"
         *     "role":"角色 0创作者 1 协作者"
         *   }
         */

        $postData = parseUserInfo(
            $postData,
            false,
            false,
        );
        $postData = json_decode($postData->getContent(), true);
        if ($postData['code'] == 200) {
            $postData = $postData['data'];
        } else {
            return $postData;
        }
        $AddUsersAndConfirmations = new AddUsersAndConfirmations($this->app);
        return $AddUsersAndConfirmations->AddUser($user_info, $postData);
    }
    public function AllowToJoin() //同意用户加入
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true,
            true,
        );
        $postData = json_decode($postData->getContent(), true);
        if ($postData['code'] == 200) {
            $postData = $postData['data'];
        } else {
            return $postData;
        }

        $AddUsersAndConfirmations = new AddUsersAndConfirmations($this->app);
        return $AddUsersAndConfirmations->AllowAddUser($user_info, $postData);
    }
    public function RefuseToJoin()
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true,
            true,
        );
        $postData = json_decode($postData->getContent(), true);
        if ($postData['code'] == 200) {
            $postData = $postData['data'];
        } else {
            return $postData;
        }

        $AddUsersAndConfirmations = new AddUsersAndConfirmations($this->app);
        return $AddUsersAndConfirmations->RefuseToJoin($user_info, $postData);
    }
    public function DeleteStatusExceptionUser() //作者拥有的权限 当用户异常状态 可以删除此用户信息
    {
        $token = cookie("Authorization");;
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true,
            true,
        );
        $postData = json_decode($postData->getContent(), true);

        $UserDeleteUser = new UserDeleteUser($this->app);
        return $UserDeleteUser->DeleteStatusExceptionUser($user_info, $postData);
    }
    public function InviteToJoin()
    {
        $token = cookie("Authorization");
        $userTokenVerificationParse = ParseUserToken($token, true);
        if ($userTokenVerificationParse['code'] != 200) {
            return json($userTokenVerificationParse);
        }
        $user_info = $userTokenVerificationParse["data"]["user_info"];

        $postData['initListType'] = input('post.initListType'); //修改的用户类型
        $postData['initListUser_id'] = input('post.initListUser_id'); //修改的用户id
        $postData['newListUser_id'] = input('post.newListUser_id'); //修改的用户id
        $postData['toKen'] = input('post.toKen'); //toKen我自己的
        $postData['bookId'] =  input('post.bookId'); //书籍id
        $postData['time'] = input('post.time'); //时间戳
        $postData['verificationBookId'] = input('post.verificationBookId'); //sha256的书籍id

        $postData = parseUserInfo(
            $postData,
            true,
            true,
        );
        $postData = json_decode($postData->getContent(), true);
        $UserInheritedUser = new UserInheritedUser($this->app);
        return $UserInheritedUser->InviteToJoin($user_info, $postData);
    }
}

/**
 * 这是一个验证 post传入数据的验证
 *
 * @param Array $postData //!前台来的数据
 * @param Array $ValidationMustBeConsistent //!验证两个传入的id 必须一致jsonDecodeUserInfo 和 jsonDecodeinitListUserInfo 里面的user_id
 * @param Array $isVerifyDefaultIser_id //!是否验证传入的用户的id 默认的 jsonDecodeUserInfo -> user_id
 * @return Json
 */
function parseUserInfo(
    $postData,
    $VerificationLevel = false,
    $isVerifyDefaultIser_id    = true
) {
    $token = cookie("Authorization");;

    $hashHmac_toKen = hash_hmac("sha256", $token .  $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
    $hashHmac_bookId = hash_hmac("sha256",   $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
    if ($hashHmac_toKen == $postData['toKen']) { //判断加密的token是不是通过

        if ($hashHmac_bookId != $postData['verificationBookId']) { //验证书籍id
            return errorJsonReturn('Validation failed', 'SeverError'); //到这里就是验证错误 验证书籍错误
        }

        if (count($postData['newListUser_id']) != 1) { //判断前台是不是选择了多个id 或者传入了多个id
            return errorJsonReturn('Array subscript exceeded', '数组下标超出 是不是有东西只能单选却多选了呐！'); //这里是 要操作的用户 只能有一个却输入了两个或两个以上
        } else {
            $postData['DecodeListUser_Info'] = authcode($postData['newListUser_id'][0], 'DECODE', config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //!解密传入的用户信息
        }

        if ($postData['DecodeListUser_Info'] == '') { //判断传入的数据是不是空的
            return errorJsonReturn('Failed to resolve user', '请尝试重新请求或刷新');
        } else {
            $postData['jsonDecodeUserInfo'] = json_decode(base64_decode($postData['DecodeListUser_Info']), true); //!base64解码要操作的用户信息
        }

        if ($postData['jsonDecodeUserInfo'] != null) {
            $tempHashHmac = hash_hmac('sha256', $postData['jsonDecodeUserInfo']['user_id'] . $postData['bookId'] . $postData['jsonDecodeUserInfo']['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id

        } else {
            return errorJsonReturn('parse user info fail', '解析用户信息失败');
        }

        if ($tempHashHmac != $postData['jsonDecodeUserInfo']['hash_hmac_user_id']) { //判断是不是 加密的信息和解析出来的用户信息一致
            return errorJsonReturn('Failed to resolve user', '解析用户信息失败');
        }

        if ($postData['initListType'][0] != null) { //判断我前台传入的创作者/协作者 第0个是不是空的
            $postData['initListType'] = $postData['initListType'][0];
            $postData['DecodeinitListUserInfo'] = authcode($postData['initListUser_id'], 'DECODE', config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //!解密传入的用户信息

        } else {
            return errorJsonReturn('Permissions Request Error');
        }

        if ($postData['DecodeinitListUserInfo'] != '' && $postData['DecodeListUser_Info'] != '') { //判断解析的用户数据 是不是空的 注意 这里是渲染时候的数据 也就是初始数据
            $postData['jsonDecodeinitListUserInfo'] = json_decode(base64_decode($postData['DecodeinitListUserInfo']), true); //!base64解码要操作的用户信息 DecodeListUser_Info这个是允许修改的默认和初始数据一样

        } else {
            return errorJsonReturn('Failed to resolve user', '请尝试重新请求或刷新');
        };

        if ($postData['jsonDecodeinitListUserInfo'] == null) { //判断初始数据 是否可以解析
            return errorJsonReturn('parse user info fail', '解析用户信息失败-新');
        }
        if ($isVerifyDefaultIser_id) {
            $tempHashHmac = hash_hmac('sha256', $postData['jsonDecodeinitListUserInfo']['user_id'] . $postData['bookId'] . $postData['time'], config("useradminConfig.useradmin_Edit_EditComicsInfo_sha256Encryption")); //加密后 加密id
            if ($tempHashHmac == $postData['jsonDecodeinitListUserInfo']['hash_hmac_user_id']) { //判断是不是 加密的信息和解析出来的用户信息一致
                //可以继续验证
            } else {
                return errorJsonReturn('Failed to resolve user', '解析用户信息失败-新');
            }
        }

        if ($VerificationLevel) { //判断是否需要验证两个id
            if (
                $postData['jsonDecodeUserInfo']['user_id'] !=
                $postData['jsonDecodeinitListUserInfo']['user_id']
            ) {
                return errorJsonReturn("Do not modify anything if you want to delete users", '如果要删除用户请不要修改任何内容');
            }
        }
        return successJsonReturn($postData);
    } else {
        return errorJsonReturn('Validation failed', 'SeverError'); //到这里就是验证错误
    }
}
